//------------------------------------------------------------------------------
#include <QtGui>
#include <QMessageBox>
#include <QInputDialog>
#include "CConfigFrm.h"
#include "CCrypt.h"
//------------------------------------------------------------------------------
CConfigFrm::CConfigFrm(CProfils *profils, QWidget *parent, Qt::WindowFlags flags) : QMainWindow(parent, flags)
{
	CProfil *profil;
	int i;

	_profils=profils;	
	setupUi(this);
	
	if((profil=_profils->getCurrentProfil()) != NULL)
	{
		lineEditBase->setText(profil->getBase());
		lineEditJasper->setText(profil->getJasper());
		
		lineEditBase->setEnabled(true);
		lineEditJasper->setEnabled(true);
	}
	
	for(i=0;i<_profils->size();i++)
	{
		QAction *act=new QAction(_profils->at(i)->getName(), this);
		
		menu_Ouvrir->setEnabled(true);
		
		act->setCheckable(_profils->size() > 1);
		act->setChecked(profil == _profils->at(i));
		menu_Ouvrir->addAction(act);
	}
}
//------------------------------------------------------------------------------
void CConfigFrm::clearCheck(void)
{
	int i;
	
	for(i=0;i<menu_Ouvrir->actions().size();i++)
	{
		menu_Ouvrir->actions().at(i)->setChecked(false);
	}
}
//------------------------------------------------------------------------------
void CConfigFrm::keyPressEvent(QKeyEvent * event)
{
	switch(event->key())
	{
		case Qt::Key_Enter:
		case Qt::Key_Return:
			on_buttonOk_clicked();
			break;
		case Qt::Key_Escape:
			on_buttonAnnuler_clicked();
			break;
		default:
			break;
	}
	
	QMainWindow::keyPressEvent(event);
}
//------------------------------------------------------------------------------
void CConfigFrm::on_buttonOk_clicked(void)
{
	CProfil *profil;
	
	if((profil=_profils->getCurrentProfil()) != NULL)
	{
		profil->setBase(lineEditBase->text());
		profil->setJasper(lineEditJasper->text());
	}
	
	hide();
}
//------------------------------------------------------------------------------
void CConfigFrm::on_buttonAnnuler_clicked(void)
{
	hide();
}
//------------------------------------------------------------------------------
void CConfigFrm::on_menu_Ouvrir_triggered(QAction *act)
{
	CProfil *profil;
	
	clearCheck();
	
	act->setChecked(true);
	
	profil=_profils->getProfil(act->text());
	lineEditBase->setText(profil->getBase());
	lineEditJasper->setText(profil->getJasper());
	
	_profils->setCurrentProfil(act->text());
}
//------------------------------------------------------------------------------
void CConfigFrm::on_actNouveau_triggered(void)
{
	QString text=QInputDialog::getText(this, tr("Nouveau profil"), tr("Nom du profil:"));
	
	if(text != "")
	{
		if(_profils->getProfil(text) != NULL)
		{	QMessageBox msgBox(	QMessageBox::Warning, tr("Attention"), 
								tr("Un profil portant ce nom existe déja !"), QMessageBox::Ok);
			msgBox.exec();
			
			return;
		}
		
		CProfil *profil=new CProfil(_profils->getSettings(), text);
		
		clearCheck();
		
		QAction *act=new QAction(text, this);
		
		menu_Ouvrir->setEnabled(true);
		
		act->setCheckable(_profils->size() != 0);
		act->setChecked(true);
		menu_Ouvrir->addAction(act);
		
		_profils->append(profil);
		_profils->setCurrentProfil(text);
		
		lineEditBase->setText(profil->getBase());
		lineEditJasper->setText(profil->getJasper());
		
		lineEditBase->setEnabled(true);
		lineEditJasper->setEnabled(true);
	}
}
//------------------------------------------------------------------------------